#ifndef role_mgr_h
#define role_mgr_h

#include <stdio.h>
#include <stdlib.h>
#include <lwan/lwan.h>
#include <lwan/lwan-mod-serve-files.h>
LWAN_HANDLER(role_mgr)
{
    response->mime_type = "text/html;charset=utf-8";
    const char *action = lwan_request_get_post_param(request, "action");
    char data_buf_fmt[256] = "{\"%s\":\"%s\"}";
    char response_buf_fmt[256] = "{\"code\":%s,\"data\":%s,\"msg\":\"%s\"}";
    if (strcmp(action, "get_one_by_kv") == 0)
    {
        const char *keyc = lwan_request_get_post_param(request, "key");
        const char *valc = lwan_request_get_post_param(request, "val");
        const char *val_type_c = lwan_request_get_post_param(request, "val_type");

        if (keyc == NULL || valc == NULL || val_type_c == NULL)
        {
            static const char noarg[] = "2";
            lwan_strbuf_set_static(response->buffer, noarg, sizeof(noarg) - 1);
            return HTTP_OK;
        }

        char buf[2048];
        /* code */
        model_role_select_one_by_kv(keyc, atoi(valc), atoi(val_type_c), buf);

        printf("buf:%s\n", buf);
        if (strlen(buf) != 0)
        {

            char code[] = "200";
            char msg[] = "";
            char data_key[64];
            char data_val[64];
            char data_buf[128];
            char response_buf_fmt_buf[256];

            sprintf(response_buf_fmt_buf, response_buf_fmt, code, buf, msg);

            lwan_strbuf_append_printf(response->buffer, response_buf_fmt_buf);
        }
        else
        {
            static const char fail[] = "1";
            lwan_strbuf_set_static(response->buffer, fail, sizeof(fail) - 1);
        }
    }
    else if (strcmp(action, "role_add") == 0)
    {
        const char *name = lwan_request_get_post_param(request, "name");
        const char *game_idc = lwan_request_get_post_param(request, "game_id");
        if (name == NULL || game_idc == NULL)
        {
            char code[] = "202";
            char msg[] = "err: params can not be null";

            char data_buf[16];
            char response_buf_fmt_buf[256];

            sprintf(data_buf, "\"\"");
            sprintf(response_buf_fmt_buf, response_buf_fmt, code, data_buf, msg);

            lwan_strbuf_append_printf(response->buffer, response_buf_fmt_buf);
        }
        int game_id = atoi(game_idc);
        char buf[2048];

        int selret = model_role_select_one_by_kv("game_id", game_id, 0, buf);
        printf("selret :%d", selret);
        if (selret != -2)
        {
            // exist
            char code[] = "203";
            char msg[] = "game_id exist";

            char data_buf[16];
            char response_buf_fmt_buf[256];

            sprintf(data_buf, "\"\"");
            sprintf(response_buf_fmt_buf, response_buf_fmt, code, data_buf, msg);

            lwan_strbuf_append_printf(response->buffer, response_buf_fmt_buf);
            return HTTP_OK;
        }
        int addret = model_role_write_one_gameid_name(game_id, name);
        fprintf(stderr, "addret = %d", addret);
        if (addret == 0)
        {
            char code[] = "200";
            char msg[] = "ok";

            char data_buf[16];
            char response_buf_fmt_buf[256];

            sprintf(data_buf, "\"\"");
            sprintf(response_buf_fmt_buf, response_buf_fmt, code, data_buf, msg);

            lwan_strbuf_append_printf(response->buffer, response_buf_fmt_buf);
        }
        else
        {
            char code[] = "201";
            char msg[] = "add err";

            char data_buf[16];
            char response_buf_fmt_buf[256];

            sprintf(data_buf, "\"\"");
            sprintf(response_buf_fmt_buf, response_buf_fmt, code, data_buf, msg);

            lwan_strbuf_append_printf(response->buffer, response_buf_fmt_buf);
        }
    }
    else if (strcmp(action, "role_update_kv_by_gameid") == 0)
    {
        const char *keyc = lwan_request_get_post_param(request, "key");
        const char *valc = lwan_request_get_post_param(request, "val");
        const char *val_typec = lwan_request_get_post_param(request, "val_type");
        const char *game_idc = lwan_request_get_post_param(request, "game_id");
        if (keyc == NULL || game_idc == NULL || valc == NULL || val_typec == NULL)
        {
            char code[] = "202";
            char msg[] = "update err: params can not be null";

            char data_buf[16];
            char response_buf_fmt_buf[256];

            sprintf(data_buf, "\"\"");
            sprintf(response_buf_fmt_buf, response_buf_fmt, code, data_buf, msg);

            lwan_strbuf_append_printf(response->buffer, response_buf_fmt_buf);
            return HTTP_OK;
        }
        int game_id = atoi(game_idc);
        int val_type = atoi(val_typec);
        if (val_type == 0)
        {
            int updateRet = model_role_update_kv_by_gameid(keyc, atoi(valc), 0, game_id);
            if (updateRet == 0)
            {
                char code[] = "200";
                char msg[] = "ok";

                char data_buf[16];
                char response_buf_fmt_buf[256];

                sprintf(data_buf, "\"\"");
                sprintf(response_buf_fmt_buf, response_buf_fmt, code, data_buf, msg);

                lwan_strbuf_append_printf(response->buffer, response_buf_fmt_buf);
            }
            else
            {
                char code[] = "201";
                char msg[] = "update err";

                char data_buf[16];
                char response_buf_fmt_buf[256];

                sprintf(data_buf, "\"\"");
                sprintf(response_buf_fmt_buf, response_buf_fmt, code, data_buf, msg);

                lwan_strbuf_append_printf(response->buffer, response_buf_fmt_buf);
            }
        }
        else
        {
            int updateRet = model_role_update_kv_by_gameid(keyc, valc, 1, game_id);
            if (updateRet == 0)
            {
                char code[] = "200";
                char msg[] = "ok";

                char data_buf[16];
                char response_buf_fmt_buf[256];

                sprintf(data_buf, "\"\"");
                sprintf(response_buf_fmt_buf, response_buf_fmt, code, data_buf, msg);

                lwan_strbuf_append_printf(response->buffer, response_buf_fmt_buf);
            }
            else
            {
                char code[] = "201";
                char msg[] = "update err";

                char data_buf[16];
                char response_buf_fmt_buf[256];

                sprintf(data_buf, "\"\"");
                sprintf(response_buf_fmt_buf, response_buf_fmt, code, data_buf, msg);

                lwan_strbuf_append_printf(response->buffer, response_buf_fmt_buf);
            }
        }
    }
    else if (strcmp(action, "role_select_cols_offset") == 0)
    {
        const char *colsc = lwan_request_get_post_param(request, "cols");
        const char *offsetc = lwan_request_get_post_param(request, "offset");
        
        if (colsc == NULL || offsetc == NULL)
        {
            char code[] = "202";
            char msg[] = "role_select_cols_offset err: params can not be null";

            char data_buf[16];
            char response_buf_fmt_buf[256];

            sprintf(data_buf, "\"\"");
            sprintf(response_buf_fmt_buf, response_buf_fmt, code, data_buf, msg);

            lwan_strbuf_append_printf(response->buffer, response_buf_fmt_buf);
            return HTTP_OK;
        }
        int offset = atoi(offsetc);
    
         char buf[2048];
        // char *buf = (char*) malloc(4096);
        int selret = model_role_select_cols_offset(colsc,offset, buf);
   
       
        if (selret == 0)
        {
            char code[] = "200";
            char msg[] = "ok";

            char data_buf[16];
           char response_buf_fmt_buf[2048];
            //char * response_buf_fmt_buf = (char*) malloc(4096);
            sprintf(data_buf, "\"\"");
            sprintf(response_buf_fmt_buf, response_buf_fmt, code, buf, msg);

            lwan_strbuf_append_printf(response->buffer, response_buf_fmt_buf);
        }
        else
        {
            char code[] = "201";
            char msg[] = "sel err";

            char data_buf[16];
            char response_buf_fmt_buf[256];

            sprintf(data_buf, "\"\"");
            sprintf(response_buf_fmt_buf, response_buf_fmt, code, data_buf, msg);

            lwan_strbuf_append_printf(response->buffer, response_buf_fmt_buf);
        }
    }
    return HTTP_OK;
}

#endif